import {observe} from "./observe.js"
import {Dep} from "./Dep.js"

export function defineReactive(data,key,val){
    //递归子元素
    let childOb=observe(val)
    //闭包中的dep

    let dep = new Dep();
    Object.defineProperty(data,key,{
        enumerable: true,
        configurable: true,
        get(){
            // console.log("getter",val);

            //处于依赖收集阶段
            if(Dep.target){
                dep.depend();
                if(childOb){
                    childOb.dep.depend();
                }
            }
            return val;
        },
        set(newValue){
            // console.log("setter"+newValue)
            val = newValue;
            //新值也要observe
            childOb=observe(newValue);
            dep.notify();
        }
    }
    )
}